<?php

namespace App\Services\Applet\Shop;

use App\Exceptions\ShopException;
use Exception;

class ShopGoodsService extends BaseShopService
{

    /**
     * 列表
     * @param array $params
     * @return array
     * @throws Exception
     */
    public function list(array $params): array
    {
        // 查询商城商品列表
        $goodsList = $this->getShopGoodsModel()->list($params, [
            'id', 'province_id', 'city_id', 'region_id', 'name', 'price', 'cover_img', 'show_sales_quantity', 'sales_quantity', 'shipping_fee',
        ]);

        $provinceIds = array_column($goodsList, 'province_id');
        $cityIds = array_column($goodsList, 'city_id');
        $countyIds = array_column($goodsList, 'region_id');

        $areaIds = array_merge($provinceIds, $cityIds, $countyIds);

        // 查询地区
        $areaList = $this->getAreaModel()->getAreaNameListByCodeList($areaIds);
        $areaListMap = array_column($areaList, 'name', 'code');

        $list = [];
        foreach ($goodsList as $item) {
            $areaName = '';
            !empty($areaListMap[$item['province_id']]) && $areaName .= $areaListMap[$item['province_id']];
            !empty($areaListMap[$item['city_id']]) && $areaName .= '-' . $areaListMap[$item['city_id']];
            !empty($areaListMap[$item['region_id']]) && $areaName .= '-' . $areaListMap[$item['region_id']];
            $item['areaName'] = $areaName;
            $list[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'price' => $item['price'] / 100,
                'areaName' => $areaName,
                'coverImg' => $item['cover_img'],
                'showSalesQuantity' => $item['show_sales_quantity'],
                'salesQuantity' => $item['sales_quantity'],
                'shippingFee' => $item['shipping_fee'] / 100,
            ];
        }

        return [
            'list' => $list
        ];
    }

    /**
     * 详情
     * @param array $params
     * @return array
     * @throws ShopException
     * @throws Exception
     */
    public function detail(array $params): array
    {
        // 查询商品详情
        $goodsDetail = $this->getShopGoodsModel()->getItemById($params['id'], [
            'id', 'province_id', 'city_id', 'region_id', 'name', 'price', 'cover_img', 'weight', 'shipping_fee', 'status', 'params', 'show_sales_quantity', 'sales_quantity'
        ]);
        if (empty($goodsDetail)) {
            throw new ShopException('商品不存在', ShopException::CODE_1001);
        }

        $areaIds = [$goodsDetail['province_id'], $goodsDetail['city_id'], $goodsDetail['region_id']];
        // 查询地区
        $areaList = $this->getAreaModel()->getAreaNameByCode($areaIds);
        $areaNameList = array_column($areaList, 'name');
        $areaName = implode('-', $areaNameList);

        return [
            'id' => $goodsDetail['id'],
            'name' => $goodsDetail['name'],
            'coverImg' => $goodsDetail['cover_img'],
            'price' => $goodsDetail['price'] / 100,
            'areaName' => $areaName,
            'weight' => $goodsDetail['weight'] / 1000, // 计算为千克
            'shippingFee' => $goodsDetail['shipping_fee'] / 100,
            'params' => json_decode($goodsDetail['params'], true) ?? [],
            'showSalesQuantity' => $goodsDetail['show_sales_quantity'],
            'salesQuantity' => $goodsDetail['sales_quantity'],
            'status' => $goodsDetail['status'],
        ];
    }

}
